package org.example.backend.common;

import lombok.Data;

/**
 * @author 啊昌
 * @date 2025/7/19 12:15
 * LoginType 登录类型：0-用户 1-管理员
 */

public enum UserType {

	USER_TYPE(0,"ROLE_USER"),
	ADMIN_TYPE(1,"ROLE_ADMIN");

	private final Integer code;
	private final String identity;

	// 枚举构造函数
	UserType(int code, String identity) {
		this.code = code;
		this.identity = identity;
	}

	// 获取角色编码
	public int getCode() {
		return code;
	}

	// 获取身份标识
	public String getIdentity() {
		return identity;
	}

	// 通过编码获取角色
	public static UserType fromCode(int code) {
		for (UserType role : values()) {
			if (role.code == code) {
				return role;
			}
		}
		throw new IllegalArgumentException("无效的角色编码: " + code);
	}

	// 通过身份标识获取角色 (不区分大小写)
	public static UserType fromIdentity(String identity) {
		for (UserType role : values()) {
			if (role.identity.equalsIgnoreCase(identity)) {
				return role;
			}
		}
		throw new IllegalArgumentException("无效的身份标识: " + identity);
	}

	// 覆盖toString()返回身份标识
	@Override
	public String toString() {
		return identity;
	}

//	public static final Integer USER = 0;
//	public static final Integer ADMIN = 1;

}
